﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Persistent;
using S4Store.Domain;
using LX.Core.Query;
using S4Store.Service.Valid.StoreValid;
using S4Store.Domain.AfterSaleKpi;
using S4Store.Domain.AfterSaleReport;

namespace S4Store.Service
{
    public class SparePartService:BaseService
    {
        public IQueryable<Domain.AfterSaleKpi.SparePartsMonthInput> GetQuery()
        {
            return this.GetQuery<SparePartsMonthInput>();
        }

        public void SubmitSparePartsMonthConfig(int year, int month, int storeid, SparePartsMonthInput config)
        {
            var query = this.GetQuery<SparePartsMonthInput>().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid).FirstOrDefault();
            //if (query != null && query.IsConfirmed) throw new Exception("确认过的数据，不能再修改！");
            if (query != null)
            {
                this.GetSet<SparePartsMonthInput>().Remove(query);
            }
            this.GetSet<SparePartsMonthInput>().Add(config);
        }

        public SPReport GetSPReport(int year, int month, int storeId)
        {
            List<SparePartsMonthInput> M = GetQuery().Where(o => o.Month.Year == year && o.Month.Month == month
                && o.StoreId == storeId && o.IsConfirmed).ToList();
            if (M == null || M.Count == 0)
            {
                M = new List<SparePartsMonthInput>();
            }


            SPReport ctr = new SPReport();
            ctr.M = M;

            return ctr;
        }

    }
}
